//
//  CredateHeZuoController.m
//  i5db
//
//  Created by iMuse on 15-5-21.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "CredateHeZuoController.h"
#import "AppCenter.h"
#import "AppButtonView.h"
#import "APPTextController.h"
#import "NotificationNavView.h"
@interface CredateHeZuoController ()<textViewDelegate>
{
    AppButtonView * locationView;
    UILabel       * xuqiuLabel;
    UILabel       * ziyuanLabel;
    
    UIScrollView * scrollow;
    UIView       * contentView;
    
    UIButton     * xuqiuButton;
    UILabel      * labelXQ;
    UIButton     * ziyuanButton;
    UILabel      * labelZY;
    
    NSLayoutConstraint * ziyuanConstraint;
    NSLayoutConstraint * xuqiuConstraint;
    NSLayoutConstraint * contentConstraint;
}

@property(nonatomic,strong) NSString* xuqiuString;
@property(nonatomic,strong) NSString* ziyuanString;

@end

@implementation CredateHeZuoController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
                self.title = @"我要合作";
                self.xuqiuString = @"";
                self.ziyuanString = @"";
    }
    return self;
}

-(void)updateViewConstraints
{
    [super updateViewConstraints];
    [scrollow autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [scrollow autoPinToBottomLayoutGuideOfViewController:self withInset:0];
    [scrollow autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [scrollow autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    
    [contentView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:scrollow withOffset:-64];
    [contentView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:scrollow withOffset:0];
    [contentView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:scrollow withOffset:0];
    [contentView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:scrollow withOffset:0];
    [contentView autoSetDimension:ALDimensionWidth toSize:SCREEN_WIDTH];
    
    
    
    
    [locationView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:15];
    [locationView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [locationView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    [locationView autoSetDimension:ALDimensionHeight toSize:40];
    
    
    [xuqiuLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:locationView withOffset:15];
    [xuqiuLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    
    [xuqiuButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:xuqiuLabel withOffset:10];
    [xuqiuButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [xuqiuButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    
    [labelXQ autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    
    [ziyuanLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:xuqiuButton withOffset:15];
    [ziyuanLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    
    [ziyuanButton autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:ziyuanLabel withOffset:10];
    [ziyuanButton autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0];
    [ziyuanButton autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0];
    
    [labelZY autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    
    
    
}
-(void) initNavigationBar
{
    [super initNavigationBar];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:@"发布" forState:UIControlStateNormal];
    
    /* [button setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateNormal];
     [button setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateSelected];
     */
    
    [button addTarget:self action:@selector(credateHeZuo:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *temporaryBarButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    temporaryBarButtonItem2.style = UIBarButtonItemStylePlain;
    
    self.navigationItem.rightBarButtonItem = temporaryBarButtonItem2;
}
-(void)credateHeZuo:(UIButton*) button
{
    [self checkEmpty];
    
}
-(BOOL) checkEmpty
{
    if (!self.xuqiuString || [self.xuqiuString isEqualToString:@""])
    {
        [NotificationNavView showMessage:@"您还没有填写需求"];
        
        return NO;
    }
    
    if (!self.ziyuanString || [self.ziyuanString isEqualToString:@""]) {
        [NotificationNavView showMessage:@"您还没有填写资源"];
        return NO;
    }
    
    return YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initNavigationBar];
    [self initView];
}
-(void) initView
{
    scrollow = [UIScrollView newAutoLayoutView];
    scrollow.showsVerticalScrollIndicator = NO;
    [self.view addSubview:scrollow];
    
    contentView = [UIView newAutoLayoutView];
 
    [scrollow addSubview:contentView];
    
    
    locationView = [AppButtonView newAutoLayoutView];
    [locationView setLeftText:@"位置"];
    [locationView setRightText:@"全国"];
    [locationView hiddenLine];
    [contentView addSubview:locationView];
    
    
    xuqiuLabel = [UILabel newAutoLayoutView];

    xuqiuLabel.text = @"需求";
    xuqiuLabel.font      = [UIFont boldSystemFontOfSize:15];
    [contentView addSubview:xuqiuLabel];
    
    xuqiuButton = [UIButton newAutoLayoutView];
    xuqiuButton.tag = 1;
    [xuqiuButton addTarget:self action:@selector(inputButton:) forControlEvents:UIControlEventTouchUpInside];
    xuqiuButton.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:xuqiuButton];
    labelXQ = [UILabel newAutoLayoutView];
    labelXQ.numberOfLines = 0;
    labelXQ.textColor = [AppCenter color:@"126,126,126,1"];
    labelXQ.text = @"1000字以内";
    labelXQ.font = [UIFont systemFontOfSize:12];
    [xuqiuButton addSubview:labelXQ];
    
    
    ziyuanLabel = [UILabel newAutoLayoutView];
    ziyuanLabel.text = @"资源";
    ziyuanLabel.font      = [UIFont boldSystemFontOfSize:15];
    [contentView addSubview:ziyuanLabel];
    
    ziyuanButton = [UIButton newAutoLayoutView];
    ziyuanButton.tag = 2;
    [ziyuanButton addTarget:self action:@selector(inputButton:) forControlEvents:UIControlEventTouchUpInside];
    ziyuanButton.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:ziyuanButton];
    labelZY = [UILabel newAutoLayoutView];
    labelZY.numberOfLines = 0;
    labelZY.text =@"1000字以内";
    labelZY.textColor = [AppCenter color:@"126,126,126,1"];
    labelZY.font = [UIFont systemFontOfSize:12];
    [ziyuanButton addSubview:labelZY];
    
   xuqiuConstraint =   [xuqiuButton autoSetDimension:ALDimensionHeight toSize:40];
   ziyuanConstraint =   [ziyuanButton autoSetDimension:ALDimensionHeight toSize:40];
   contentConstraint = [contentView autoSetDimension:ALDimensionHeight toSize:SCREEN_HEIGHT-64];
    
    
}
-(void) inputButton:(UIButton*) button
{
    NSInteger tag = button.tag;
    NSString * title = nil;
    NSString * text  = nil;
    if (tag == 1) {
        title = @"需求";
        text = self.xuqiuString;
    }else
    {
        title = @"资源";
        text = self.ziyuanString;
    }
    
    
    APPTextController * textController = [[APPTextController alloc] initWith:1000 minNumber:0 tag:[NSString stringWithFormat:@"%d",button.tag] title:title text:text];
    textController.delegate = self;
    
    [self.navigationController pushViewController:textController animated:YES];
}
-(void) textViewString:(NSString *)tag text:(NSString *)text
{
    NSInteger tagInteger = [tag integerValue];
    CGSize size = [AppCenter sizeWithString:text font:labelXQ.font maxHeight:2000 maxWeight:SCREEN_WIDTH];
    
    if (size.height<40) size.height = 40;
    
    if (tagInteger==1)
    {
        labelXQ.text = text;
        self.xuqiuString = text;
        
        xuqiuConstraint.constant = size.height+40;
    }else if (tagInteger == 2)
    {
        labelZY.text = text;
        self.ziyuanString = text;
        ziyuanConstraint.constant = size.height+40;
    }
    
    contentConstraint.constant = xuqiuConstraint.constant+ziyuanConstraint.constant+105+64;
    
    
}
- (void)viewWillAppear:(BOOL)animated {
    
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:NO];
    [super viewWillAppear:animated];
    
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)viewWillDisappear:(BOOL)animated {
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:NO];
    
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
 
}



@end
